/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SRC_CPP_RTPS_TRANSPORT_UDSDGRAMSENDERRESOURCE_HPP_
#define SRC_CPP_RTPS_TRANSPORT_UDSDGRAMSENDERRESOURCE_HPP_
#include <ertps/utils/LocatorAlias.h>
#include <transport/SenderResource.h>
#include "UDSDgramTransportInterface.h"
#include <vector>
#include <string>
namespace vbs {
namespace transport {
class UDSDgramSenderResource : public vbs::transport::SenderResource {
 public:
    UDSDgramSenderResource(UDSDgramTransportInterface& transport, asio::local::datagram_protocol::socket& socket,
                           uint32_t order = 65535)
        : SenderResource(transport.kind()), socket_(std::move(socket)) {
        clean_up = [this, &transport]() {
            transport.CloseOutputChannel(socket_);
        };
        send_lambda_ = [this, &transport](octet* data, uint32_t dataSize,
                                          vbsutil::xmlparser::LocatorsIterator* destination_locators_begin,
                                          vbsutil::xmlparser::LocatorsIterator* destination_locators_end) -> bool {
            return transport.send(data, dataSize, socket_, destination_locators_begin, destination_locators_end);
        };
        order_ = order;
    }
    ~UDSDgramSenderResource() noexcept {
        try {
            std::string remove_path = socket_.local_endpoint().path();
            socket_.close();
            if (std::remove(remove_path.c_str()) == 0) {
                logInfo(TRANSPORT, "Delete the send resource, path: " << remove_path);
            } else {
                logInfo(TRANSPORT, "Failed to delete the send resource!!! path: " << remove_path);
            }
        } catch (const std::exception& e) {
            logError(TRANSPORT, "Destructor exception: " << e.what());
        }
    }
    static UDSDgramSenderResource* cast(TransportInterface& transport, SenderResource* sender_resource) {
        UDSDgramSenderResource* returned_resource = nullptr;
        if (sender_resource->kind() == transport.kind()) {
            returned_resource = dynamic_cast<UDSDgramSenderResource*>(sender_resource);
        }
        return returned_resource;
    }

 private:
    UDSDgramSenderResource() = delete;
    UDSDgramSenderResource(const SenderResource&) = delete;
    UDSDgramSenderResource& operator=(const SenderResource&) = delete;
    asio::local::datagram_protocol::socket socket_;
};
}  // namespace transport
}  // namespace vbs
#endif
